<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="projectCode">
        <el-input
          v-model="queryParams.projectCode"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:projectexpertconfig:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:projectexpertconfig:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:projectexpertconfig:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:projectexpertconfig:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectexpertconfigList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="项目编号" align="center" prop="projectCode" />
      <el-table-column label="专家地区" align="center" prop="expertAreaName" />
      <el-table-column label="回避专家" align="center" prop="noExpertNames" />
      <el-table-column label="设定专家" align="center" prop="expertNames" />
      <el-table-column label="自定义专家组" align="center" prop="expertGroupName" />
      <el-table-column label="专家总数" align="center" prop="expertNum" />
      <el-table-column label="抽取次数" align="center" prop="extractNum" />
      <el-table-column label="抽取状态" prop="extractStatus"  width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.extractStatus">已抽完</el-tag>
          <el-tag v-if="!scope.row.extractStatus" type="info">抽取中</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
             v-if="!scope.row.extractStatus"
            size="mini"
            type="text"
            @click="handleExtract(scope.row)"
            v-hasPermi="['system:projectexpertconfig:edit']"
          >抽取</el-button>
          <el-button
             v-if="scope.row.extractStatus"
            size="mini"
            type="text"
            @click="showExtractRecord(scope.row)"
            v-hasPermi="['system:projectexpertconfig:edit']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:projectexpertconfig:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:projectexpertconfig:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专家抽取设置2对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="项目名称" prop="projectName">
          <el-input v-model="form.projectName" placeholder="请选择项目" @focus="handleProject"/>
        </el-form-item>
        <el-form-item label="专家地区" prop="expertAreaName">
          <el-select v-model="form.expertAreaId" @change="selectArea" clearable placeholder="请选择">
              <el-option
                v-for="item in areaList"
                :key="item.areaId"
                :label="item.areaName"
                :value="item.areaId">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="回避专家" prop="noExpertNames">
          <el-select v-model="form.noExpertIds" multiple @change="selectNoExpert" placeholder="请选择">
              <el-option
                v-for="item in expertList"
                :key="item.id"
                :label="item.expertName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="设定专家" prop="expertNames">
          <el-select v-model="form.expertIds" multiple @change="selectExpert" placeholder="请选择">
              <el-option
                v-for="item in expertList"
                :key="item.id"
                :label="item.expertName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="设定专家组" prop="expertGroupName">
          <el-select v-model="form.expertGroupId" multiple @change="selectExpertGroup" placeholder="请选择">
              <el-option
                v-for="item in expertList"
                :key="item.id"
                :label="item.expertName"
                :value="item.id">
              </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="抽取次数" prop="extractNum">
          <el-input v-model="form.extractNum" placeholder="请输入抽取次数" />
        </el-form-item>
        <el-form-item label="专家总数" prop="expertNum">
          <el-input v-model="form.expertNum" placeholder="请输入专家总数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 项目列表 -->
    <el-dialog :title="projectTitle" :visible.sync="projectOpen" append-to-body>
      <el-form :model="projectParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目编号" prop="projectCode">
          <el-input
            v-model="projectParams.projectCode"
            placeholder="请输入项目编号"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="projectParams.projectName"
            placeholder="请输入项目名称"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目部门" prop="projectDeptId">
          <treeselect v-model="projectParams.projectDeptId" :options="deptList" :show-count="true" placeholder="请选择归属部门" style="width: 130px"/>
        </el-form-item>
        <el-form-item label="资质主体" prop="projectAgencyId">
          <el-input
            v-model="projectParams.projectAgencyId"
            placeholder="请输入资质主体"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleProjectQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="projectList">
        <el-table-column label="ID" align="center" prop="id"  width="55"/>
        <el-table-column label="项目名称" align="center" prop="projectName" />
        <el-table-column label="项目编号" align="center" prop="projectCode" />
        <el-table-column label="项目外部编号" align="center" prop="projectExternaltCode" />
        <el-table-column label="项目类型" align="center" prop="projectTypeName" />
        <el-table-column label="项目所属部门" align="center" prop="projectDeptName" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleSelectProject(scope.row)"
              v-hasPermi="['system:project:edit']"
            >选择</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="projectTotal>0"
        :total="projectTotal"
        :page.sync="projectParams.pageNum"
        :limit.sync="projectParams.pageSize"
        @pagination="getProjectList"
      />
    </el-dialog>

    <!-- 专家列表 -->
    <el-dialog :title="expertTitle" :visible.sync="expertOpen" append-to-body>
      <el-form :model="expertParams" ref="expertForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="项目编号" prop="projectCode">
          <el-input
            v-model="expertParams.projectCode"
            placeholder="请输入项目编号"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="projectName">
          <el-input
            v-model="expertParams.projectName"
            placeholder="请输入项目名称"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item label="项目部门" prop="projectDeptId">
          <treeselect v-model="expertParams.projectDeptId" :options="deptList" :show-count="true" placeholder="请选择归属部门" style="width: 130px"/>
        </el-form-item>
        <el-form-item label="资质主体" prop="projectAgencyId">
          <el-input
            v-model="expertParams.projectAgencyId"
            placeholder="请输入资质主体"
            clearable
            style="width: 130px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleExpertQuery">搜索</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="expertList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="id" />
        <el-table-column label="专家姓名" align="center" prop="expertName" />
        <el-table-column label="手机号码" align="center" prop="mobile" />
        <el-table-column label="证件类型" align="center" prop="identityTypeName" />
        <el-table-column label="证件号码" align="center" prop="identityCode" />
        <el-table-column label="地区" align="center" prop="areaName" />
        <el-table-column label="专家类型" align="center" prop="expertTypeName" />
        <el-table-column label="性别" prop="sex"  width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sex === 1">男</el-tag>
            <el-tag v-if="scope.row.sex === 2" type="success">女</el-tag>
            <el-tag v-if="scope.row.sex === 0" type="info">女</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="专家品目" align="center" prop="expertCategoryName" />
        <el-table-column label="状态" prop="status"  width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status">正常</el-tag>
            <el-tag v-if="!scope.row.status" type="info">停用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="审批状态" align="center" prop="approvalStatus" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:expert:edit']"
            >修改</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="expertTotal>0"
        :total="expertTotal"
        :page.sync="expertParams.pageNum"
        :limit.sync="expertParams.pageSize"
        @pagination="getExpertList"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listProjectexpertconfig, getProjectexpertconfig, delProjectexpertconfig, addProjectexpertconfig, updateProjectexpertconfig, extractProjectexpertconfig } from "@/api/system/projectexpertconfig";
import { listsCompanyarea} from "@/api/system/companyarea";
import { listProject} from "@/api/system/project";
import { deptTreeSelect } from "@/api/system/user";
import { listExpert } from "@/api/system/expert";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";


export default {
  name: "Projectexpertconfig",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目总条数
      projectTotal: 0,
      // 专家总条数
      expertTotal: 0,
      // 专家抽取设置表格数据
      projectexpertconfigList: [],
      // 地区列表
      areaList: [],
      // 部门列表
      deptList: [],
      // 项目列表
      projectList: [],
      // 专家列表
      expertList: [],
      // 弹出层标题
      title: "",
      // 专家标题
      expertTitle: "",
      // 是否显示弹出层
      open: false,
      // 项目弹出层标题
      projectTitle: "",
      // 项目是否显示弹出层
      projectOpen: false,
      // 专家是否显示弹出层
      expertOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: null,
        projectName: null,
        projectCode: null,
        noExpertIds: null,
        noExpertNames: null,
        expertIds: null,
        expertNames: null,
        expertGroupId: null,
        expertGroupName: null,
        extractNum: null,
        extractStatus: null,
        expertAreaId: null,
        expertAreaName: null,
        expertNum: null,
      },
      // 项目参数
      projectParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 项目参数
      expertParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getAreaList();
    this.getProjectList();
    this.getDeptTree();
    this.getExpertList();
  },
  methods: {
    /** 查询专家抽取设置2列表 */
    getList() {
      this.loading = true;
      listProjectexpertconfig(this.queryParams).then(response => {
        this.projectexpertconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 获取地区列表
    getAreaList(){
      this.loading = true;
      listsCompanyarea(this.queryParams).then(response => {
        this.areaList = response.data;
        this.loading = false;
      });
    },
    // 获取项目列表
    getProjectList(){
      this.loading = true;
      listProject(this.projectParams).then(response => {
        this.projectList = response.rows;
        this.projectTotal = response.total;
        this.loading = false;
      });
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptList = response.data;
      });
    },
    // 专家列表
    getExpertList(){
      this.loading = true;
      listExpert(this.expertParams).then(response => {
        this.expertList = response.rows;
        this.expertTotal = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        projectName: null,
        projectCode: null,
        noExpertIds: null,
        noExpertNames: null,
        expertIds: null,
        expertNames: null,
        expertGroupId: null,
        expertGroupName: null,
        extractNum: null,
        extractStatus: null,
        expertAreaId: null,
        expertAreaName: null,
        expertNum: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专家抽取设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProjectexpertconfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改专家抽取设置";

        // 数据转换
        if(this.form.expertGroupId){
          this.form.expertGroupId = this.form.expertGroupId.split(',');
        }

        if(this.form.noExpertIds){
          this.form.noExpertIds = this.form.noExpertIds.split(',');
        }

        if(this.form.expertIds){
          this.form.expertIds = this.form.expertIds.split(',');
        }


      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 数据处理
          if(this.form.expertGroupId.length > 0){
            this.form.expertGroupId = this.form.expertGroupId.join(',');
          }

          if(this.form.expertIds.length > 0){
            this.form.expertIds = this.form.expertIds.join(',');
          }

          if(this.form.noExpertIds.length > 0){
            this.form.noExpertIds = this.form.noExpertIds.join(',');
          }

          if (this.form.id != null) {
            updateProjectexpertconfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProjectexpertconfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除专家抽取设置编号为"' + ids + '"的数据项？').then(function() {
        return delProjectexpertconfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/projectexpertconfig/export', {
        ...this.queryParams
      }, `projectexpertconfig_${new Date().getTime()}.xlsx`)
    },
    handleProject(){
      this.projectOpen = true;
      this.projectTitle = "选择项目";
    },
    handleSelectProject(row){
      this.form.projectId = row.id;
      this.form.projectName = row.projectName;
      this.form.projectCode = row.projectCode;
      this.projectOpen=false;
    },
    selectArea(value) {
      // 通过已选的value值找到对应的对象
      const selectedOption = this.areaList.find(option => option.areaId === value);
      if (selectedOption) {
        this.form.expertAreaName = selectedOption.areaName;
      }
      this.projectParams.areaId = value;
    },
    /** 搜索按钮操作 */
    handleExpertQuery() {
      this.expertParams.pageNum = 1;
      this.getExpertList();
    },
    handleProjectQuery() {
      this.projectParams.pageNum = 1;
      this.getProjectList();
    },
    selectNoExpert(value) {
      const selectedOptions = this.expertList.filter(option =>
        value.includes(option.id)
      );
      const selectedProps = selectedOptions.map(option => option.expertName);
      this.form.noExpertNames = selectedProps.join(',');
    },
    selectExpert(value) {
      const selectedOptions = this.expertList.filter(option =>
        value.includes(option.id)
      );
      const selectedProps = selectedOptions.map(option => option.expertName);
      this.form.expertNames = selectedProps.join(',');
    },
    selectExpertGroup(value) {
      const selectedOptions = this.expertList.filter(option =>
        value.includes(option.id)
      );
      const selectedProps = selectedOptions.map(option => option.expertName);
      this.form.expertGroupName = selectedProps.join(',');
    },
    handleExtract(row) {
      const id = row.id;
      this.$modal.confirm('是否确认抽取专家？').then(function() {
        return extractProjectexpertconfig(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("专家抽取成功");
      }).catch(() => {});
    },
    showExtractRecord(row) {
      const id = row.id;
    },
  }
};
</script>
